package requeteur.ihm.bean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import org.apache.commons.lang3.StringUtils;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

import requeteur.ejb.commun.dto.CleEtrangere;
import requeteur.ejb.commun.dto.InformationBaseDeDonnees;
import requeteur.ejb.commun.dto.InformationConnection;
import requeteur.ejb.commun.dto.ResultatRequete;
import requeteur.ejb.commun.dto.Table;
import requeteur.ejb.commun.exception.ConnectionException;
import requeteur.ejb.service.SrvLancerRequete;
import requeteur.ejb.service.SrvRecupererInformation;
import requeteur.ihm.outil.JsfUtils;

/**
 * @author Laurent
 * @version 0.1
 */
@ManagedBean
@SessionScoped
public class BeanRequeteur {

	@ManagedProperty(value = "#{beanApplication}")
	private BeanApplication beanApplication;

	@EJB
	private SrvRecupererInformation srvRecupererInformation;
	@EJB
	private SrvLancerRequete srvLancerRequete;

	private CleEtrangere cleEtrangere;
	private ResultatRequete resultatDonnees;
	private ResultatRequete resultatRequete;
	private TreeNode treeNode;
	private String ecran;
	private String requete;
	private Table table;
	private InformationConnection informationConnection = new InformationConnection();
	private InformationBaseDeDonnees informationBaseDeDonnees;

	/**
	 * Constructeur de la classe <code>BeanRequeteur</code>.
	 */
	public BeanRequeteur() {
	}

	@PostConstruct
	private void init() {
		informationConnection.setUrl("jdbc:mysql://localhost:3306/sakila");
		informationConnection.setLogin("root");
		informationConnection.setPassword("Kestabfc1234*");
		informationConnection.setConnected(true);
		treeNode = new DefaultTreeNode(null, null);
		try {
			for (Table vTable : srvRecupererInformation.listerTables(informationConnection)) {
				new DefaultTreeNode("table", vTable, treeNode);
			}
		} catch (ConnectionException e) {
			JsfUtils.addGlobalErrorMessage(e.getMessage() + " : " + e.getCause().getMessage());
		}
	}

	public void selectionTable(NodeSelectEvent event) {
		table = (Table) event.getTreeNode().getData();
		listerChamps();
	}

	public void listerChamps() {
		try {
			table.setListeChamp(srvRecupererInformation.listerChamps(table, informationConnection));
			ecran = "informations";
		} catch (ConnectionException e) {
			JsfUtils.addGlobalErrorMessage(e.getMessage() + " : " + e.getCause().getMessage());
		}
	}

	public void executerRequete(String pRequete) {
		try {
			resultatRequete = srvLancerRequete.lancerRequete(pRequete, informationConnection);
		} catch (ConnectionException e) {
			JsfUtils.addGlobalErrorMessage(e.getMessage() + " : " + e.getCause().getMessage());
		}
	}

	public void executerScript(String pScript) {
		try {
			resultatRequete = srvLancerRequete.lancerScript(pScript, informationConnection);
		} catch (ConnectionException e) {
			JsfUtils.addGlobalErrorMessage(e.getMessage() + " : " + e.getCause().getMessage());
		}
	}

	public void afficherDonnees() {
		if (table != null) {
			try {
				resultatDonnees = srvLancerRequete.lancerRequeteSelectAll(table, informationConnection);
			} catch (ConnectionException e) {
				JsfUtils.addGlobalErrorMessage(e.getMessage() + " : " + e.getCause().getMessage());
			}
		}
	}

	public List<String> complete(final String token, final String context) {
		final List<String> suggestions = new ArrayList<String>();
		try {
			for (Table vTable : srvRecupererInformation.listerTables(informationConnection)) {
				if (StringUtils.startsWithIgnoreCase(vTable.getNom(), token) || token == null) {
					suggestions.add(vTable.getNom().toLowerCase());
				}
			}
			Collections.sort(suggestions);
		} catch (ConnectionException e) {
			JsfUtils.addGlobalErrorMessage(e.getMessage() + " : " + e.getCause().getMessage());
		}
		return suggestions;
	}
	
	public InformationBaseDeDonnees getInformationBaseDeDonnees() {
		InformationBaseDeDonnees vInformationBaseDeDonnees = informationBaseDeDonnees;
		if (vInformationBaseDeDonnees == null) {
			try {
				vInformationBaseDeDonnees = srvRecupererInformation.getInformationBaseDeDonnees(informationConnection);
			} catch (ConnectionException e) {
				e.printStackTrace();
			}
		}
		return vInformationBaseDeDonnees;
	}

	public BeanApplication getBeanApplication() {
		return beanApplication;
	}

	public void setBeanApplication(BeanApplication pBeanApplication) {
		beanApplication = pBeanApplication;
	}

	public CleEtrangere getCleEtrangere() {
		return cleEtrangere;
	}

	public void setCleEtrangere(CleEtrangere pCleEtrangere) {
		cleEtrangere = pCleEtrangere;
	}

	public ResultatRequete getResultatRequete() {
		return resultatRequete;
	}

	public void setResultatRequete(ResultatRequete pResultatRequete) {
		resultatRequete = pResultatRequete;
	}

	public ResultatRequete getResultatDonnees() {
		return resultatDonnees;
	}

	public void setResultatDonnees(ResultatRequete pResultatDonnees) {
		resultatDonnees = pResultatDonnees;
	}

	public TreeNode getTreeNode() {
		return treeNode;
	}

	public void setTreeNode(TreeNode pTreeNode) {
		treeNode = pTreeNode;
	}

	public String getEcran() {
		return ecran;
	}

	public void setEcran(String pEcran) {
		ecran = pEcran;
	}

	public String getRequete() {
		return requete;
	}

	public void setRequete(String pRequete) {
		requete = pRequete;
	}

	public Table getTable() {
		return table;
	}

	public void setTable(Table pTable) {
		table = pTable;
	}

	public InformationConnection getInformationConnection() {
		return informationConnection;
	}

	public void setInformationConnection(InformationConnection informationConnection) {
		this.informationConnection = informationConnection;
	}

}
